Assignment operators are used to assign values to variables. Here are the different types of assignment operators in Python:
⮞
Assign (=): This operator is used to assign the value of the right side of the expression to the left side operand. For example, x = y + z assigns the value of y + z to x.
⮞
Add and Assign (+=): This operator adds the right side operand to the left side operand and then assigns the result to the left operand. For example, x += y is equivalent to x = x + y.
⮞
Subtract and Assign (-=): This operator subtracts the right operand from the left operand and then assigns the result to the left operand. For example, x -= y is equivalent to x = x - y.
⮞
Multiply and Assign (*=): This operator multiplies the right operand with the left operand and then assigns the result to the left operand. For example, x *= y is equivalent to x = x * y.
⮞
Divide and Assign (/=): This operator divides the left operand by the right operand and then assigns the result to the left operand. For example, x /= y is equivalent to x = x / y.
⮞
Modulus and Assign (%=): This operator takes the modulus using the left and the right operands and then assigns the result to the left operand. For example, x %= y is equivalent to x = x % y.
⮞
Floor Division and Assign (//=): This operator performs floor division on operands and assigns the result to the left operand. For example, x //= y is equivalent to x = x // y.
⮞
Exponent and Assign (**=): This operator raises the left operand to the power of the right operand and assigns the result to the left operand. For example, x **= y is equivalent to x = x ** y.
⮞
Bitwise AND and Assign (&=): This operator performs bitwise AND on operands and assigns the result to the left operand. For example, x &= y is equivalent to x = x & y.
⮞
Bitwise OR and Assign (|=): This operator performs bitwise OR on operands and assigns the result to the left operand. For example, x |= y is equivalent to x = x | y.
⮞
Bitwise XOR and Assign (^=): This operator performs bitwise XOR on operands and assigns the result to the left operand. For example, x ^= y is equivalent to x = x ^ y.
⮞
Bitwise Right Shift and Assign (>>=): This operator performs bitwise right shift on operands and assigns the result to the left operand. For example, x >>= y is equivalent to x = x >> y.
⮞
Bitwise Left Shift and Assign (<<=): This operator performs bitwise left shift on operands and assigns the result to the left operand. For example, x <<= y is equivalent to x = x << y.
Examples of each of the assignment operators in python
# Assign (=)
x = 10
print(x)
# Add and Assign (+=)
x += 5
print(x)
# Subtract and Assign (-=)
x -= 5
print(x)
# Multiply and Assign (*=)
x *= 2
print(x)
# Divide and Assign (/=)
x /= 2
print(x)
# Modulus and Assign (%=)
x %= 3
print(x)
# Floor Division and Assign (//=)
x = 10
x //= 3
print(x)
# Exponent and Assign (**=)
x **= 2
print(x)
# Bitwise AND and Assign (&=)
x &= 2
print(x)
# Bitwise OR and Assign (|=)
x |= 1
print(x)
# Bitwise XOR and Assign (^=)
x ^= 1
print(x)
# Bitwise Right Shift and Assign (>>=)
x = 10
x >>= 2
print(x)
# Bitwise Left Shift and Assign (<<=)
x <<= 2
print(x)
Output
10
15
10
20
10.0
1.0
3
9
0
1
0
2
8
In these examples, the value of x is updated after each operation. The print(x) statement is used to display the value of x after each operation. Please note that the bitwise operators (&=, |=, ^=, >>=, <<=) work with integers in binary format.